﻿if (!Seq) Seq = new Object();

Seq.Pattern = Class.create();
Seq.Pattern.prototype = Object.extend(Seq.Viewport.prototype, {

	_patterndata: {},
	_onChange: null,
	_patternid: '',

	initialize: function(opts) {
		var def = { onChange: null };
		opts = Object.extend(def, opts);
		this._patterndata = { duration: 0, notes: [], automation: [] };
		this._onChange = opts.onChange;

		Seq.Notifications.Observe('pattern-changed', this.patternChangeNotification.bindAsEventListener(this));
	},

	_fireOnChange: function() {
		this._onChange(this);
	},

	patternChangeNotification: function(cmd, args) {
		if (args[0] == this._patternid) {
			this.beginReloadPattern();
			this._fireOnChange();
		}
	},

	onPatternLoaded: function(r) {
		var data = r.responseText.evalJSON(false);
		this._patterndata = data;
		this._fireOnChange();
	},

	beginReloadPattern: function() {
		new Ajax.Request('/api/call', {
			method: 'get',
			parameters: { 'getpattern': this._patternid },
			onSuccess: this.onPatternLoaded.bindAsEventListener(this)
		});
	},

	loadPattern: function(patid) {
		this._patternid = patid;
		this.beginReloadPattern();

	},

	getNote: function(noteid) {
		var ret = {};
		return ret;
	},

	getNotesByStart: function(start) {
		var ret = new Array();
		for (var j = 0; j < this._patterndata.notes.length; j++) {
			var n = this._patterndata.notes[j];
			if (n.from == start)
				ret.push(n);
		}
		return ret;
	},

	getNotesByStartAndKey: function(start, key) {
		var ret = new Array();
		for (var j = 0; j < this._patterndata.notes.length; j++) {
			var n = this._patterndata.notes[j];
			if (n.from == start && n.note == key)
				ret.push(n);
		}
		return ret;
	},

	//	getPatternData: function() {
	//		return this._patterndata;
	//	},

	getAllNotes: function() {
		return this._patterndata.notes;
	},

	//	setPatternData: function(data) {
	//		this._patterndata = data;
	//		this._fireOnChange();
	//	},

	setAllNotes: function(data) {
		this._patterndata.notes = data;
		this._fireOnChange();
	},

	modifyNote: function(noteid, notedata) {
		//	notify( 'movenote', { id:lastnoteblock.id, pattern:pat, note:lastnoteblock.note, from:lastnoteblock.from, to:lastnoteblock.to, velocity:100 } );
		var p = notedata;
		p.id = noteid;
		p.pattern = this._patternid;
		Seq.Notifications.Send('modifynote', p);
		for (var j = this._patterndata.notes.length - 1; j >= 0; j--) {
			if (this._patterndata.notes[j].id == noteid)
				this._patterndata.notes[j] = Object.extend(this._patterndata.notes[j], notedata);
		}
		this._fireOnChange();
		return true;
	},

	deleteNote: function(noteid) {
		Seq.Notifications.Send('deletenote', { id: noteid, pattern: this._patternid });
		for (var j = this._patterndata.notes.length - 1; j >= 0; j--) {
			if (this._patterndata.notes[j].id == noteid)
				this._patterndata.notes.splice(j, 1);
		}
		this._fireOnChange();
		return true;
	},

	createNote: function(notedata) {
		// notify( 'addnote', { id:not.id, pattern:pat, note:not.note, from:not.from, to:not.to, velocity:not.velocity } );
		var p = notedata;
		p.id = Seq.Common.GetRandomID('note');
		p.pattern = this._patternid;
		// p.session = this._ses
		Seq.Notifications.Send('addnote	', p);
		this._patterndata.notes.push(p);
		this._fireOnChange();
		return true;
	}

});


























